package cn.iocoder.yudao.module.erp.service.relationship;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.relationship.vo.RelationshipPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.relationship.vo.RelationshipRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.relationship.vo.RelationshipSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpSupplierDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.relationship.RelationshipDO;
import cn.iocoder.yudao.module.erp.dal.mysql.product.ErpProductMapper;
import cn.iocoder.yudao.module.erp.dal.mysql.purchase.ErpSupplierMapper;
import cn.iocoder.yudao.module.erp.dal.mysql.relationship.RelationshipMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.RELATIONSHIP_NOT_EXISTS;

/**
 * 物料供应商关联 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class RelationshipServiceImpl implements RelationshipService {

    @Resource
    private RelationshipMapper relationshipMapper;
    @Resource
    private ErpProductMapper productMapper;
    @Resource
    private ErpSupplierMapper supplierMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createRelationship(RelationshipSaveReqVO createReqVO) {
        // 插入
        RelationshipDO relationship = BeanUtils.toBean(createReqVO, RelationshipDO.class);
        relationshipMapper.insert(relationship);

        // 返回
        return relationship.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRelationship(RelationshipSaveReqVO updateReqVO) {
        // 校验存在
        validateRelationshipExists(updateReqVO.getId());
        // 更新
        RelationshipDO updateObj = BeanUtils.toBean(updateReqVO, RelationshipDO.class);
        relationshipMapper.updateById(updateObj);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRelationship(Long id) {
        // 校验存在
        validateRelationshipExists(id);
        // 删除
        relationshipMapper.deleteById(id);

        // 删除子表
        deleteProductById(id);
        deleteSupplierById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRelation(Long id) {
        // 删除
        relationshipMapper.deleteByList(id);
    }

    private void validateRelationshipExists(Long id) {
        if (relationshipMapper.selectById(id) == null) {
            throw exception(RELATIONSHIP_NOT_EXISTS);
        }
    }

    @Override
    public RelationshipDO getRelationship(Long id) {
        return relationshipMapper.selectById(id);
    }

    @Override
    public List<RelationshipDO> getRelationships(List<Long> ids) {
        return relationshipMapper.selectByIds(ids);
    }

    @Override
    public List<RelationshipDO> getRelationshipsByProductIds(List<Long> productIds) {
        return relationshipMapper.selectByProductIds(productIds);
    }

    @Override
    public PageResult<RelationshipDO> getRelationshipPage(RelationshipPageReqVO pageReqVO) {
        return relationshipMapper.selectPage(pageReqVO);
    }

    // ==================== 子表（物料字典） ====================

    @Override
    public ErpProductDO getProductById(Long id) {
        return productMapper.selectById(id);
    }

    private void createProduct(Long id, ErpProductDO product) {
        if (product == null) {
            return;
        }
        product.setId(id);
        productMapper.insert(product);
    }

    private void updateProduct(Long id, ErpProductDO product) {
        if (product == null) {
            return;
        }
        product.setId(id);
        product.setUpdater(null).setUpdateTime(null); // 解决更新情况下：updateTime 不更新
        productMapper.insertOrUpdate(product);
    }

    private void deleteProductById(Long id) {
        productMapper.deleteById(id);
    }

    // ==================== 子表（供应商管理） ====================

    @Override
    public ErpSupplierDO getSupplierById(Long id) {
        return supplierMapper.selectById(id);
    }

    @Override
    public List<String> selectRelation(Long id) {
        return relationshipMapper.selectBySupplierId(id);
    }

    @Override
    public List<RelationshipRespVO> getSupplie(Long id) {
        return relationshipMapper.getSupplie(id);
    }

    private void createSupplier(Long id, ErpSupplierDO supplier) {
        if (supplier == null) {
            return;
        }
        supplier.setId(id);
        supplierMapper.insert(supplier);
    }

    private void updateSupplier(Long id, ErpSupplierDO supplier) {
        if (supplier == null) {
            return;
        }
        supplier.setId(id);
        supplier.setUpdater(null).setUpdateTime(null); // 解决更新情况下：updateTime 不更新
        supplierMapper.insertOrUpdate(supplier);
    }

    private void deleteSupplierById(Long id) {
        supplierMapper.deleteById(id);
    }

}